import AppConstants from "../framework/scripts/AppConstants"
import TimerMgr from "../framework/scripts/manager/TimerMgr"
import StateMachine from "../framework/scripts/state/StateMachine"
import PanelBase from "../framework/scripts/view/PanelBase"
import StateDef from "./state/StateDef"
import StateLeftRight from "./state/StateLeftRight"
import StateUpDown from "./state/StateUpDown"


export default class PanelStateMachine extends PanelBase {
    public skinPath: string = 'PanelStateMachine'

    public panelMaskStyle: number = AppConstants.panelMaskStyle.Close | AppConstants.panelMaskStyle.Black //关闭组件(点击面板区域外会关闭面板)加半透明组件
    public panelShowStyle: number = AppConstants.panelShowStyle.Normal

    private _machine: StateMachine


    protected onInit(): void {
        this._machine = new StateMachine(this)

        this._machine.register(new StateLeftRight(this))
        this._machine.register(new StateUpDown(this))
    }

    protected onInitDone(): void {
        this._machine.enter(StateDef.leftRight)

        TimerMgr.inst.add(0, this.update, this, 0)
    }

    protected onButtonClick(button: cc.Node) {
        if (button.name == 'BtnClose') {
            this.close()
        } else if (button.name == 'BtnLR') {
            this._machine.enter(StateDef.leftRight)
        } else if (button.name == 'BtnUD') {
            this._machine.enter(StateDef.upDown)
        }
    }

    protected onDestroy(): void {
        TimerMgr.inst.clear(this)
    }


    private update(dt: number) {
        this._machine.update(dt)
    }
}
